package sword_to_1;

public class LeeCode_16 {
    public static void main(String[] args) {
        System.out.println(myPow(2.1, 3));
        System.out.println(myPow(2, -2));
        System.out.println(myPow(0.1, 1000000000));
    }
    private static double myPow(double x, int n2) {
        long n = n2;
        if (n == 0)
            return 1;
        boolean flag = false;
        if (n < 0){
            flag = true;
            n = -n;
        }
        return flag ? 1 / pow(x, n) : pow(x, n);
    }
    private static double pow(double x, long n){
        if (n == 1){
            return x;
        }
        double t = n % 2 == 1 ? x : 1;
        n /= 2;
        double pow = pow(x, n);
        return pow * pow * t;
    }
}
